package com.game.client.command;


import com.game.client.service.GameClientBoot;
import com.game.client.service.GameClientConfig;
import com.game.netty.game.message.ConfirmMesgRequest;
import com.game.netty.game.message.FirstMsgRequest;
import com.game.netty.game.message.SecondMsgRequest;
import com.game.netty.game.message.business.BuyArenaChallengeTimesMsgRequest;
import com.game.netty.game.message.business.EnterGameMsgRequest;
import com.game.netty.game.message.business.GetPlayerByIdMsgRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellOption;

@ShellComponent
public class GameClientCommand {
    @Autowired
    private GameClientBoot gameClientBoot;
    @Autowired
    private GameClientConfig gameClientConfig;
    private Logger logger = LoggerFactory.getLogger(GameClientCommand.class);

    @ShellMethod("连接服务器，格式：connect-server  [host] [port]")//连接服务器命令，
    public void connectServer(@ShellOption(defaultValue= "")String host,@ShellOption(defaultValue = "0")int port) {
        if(!host.isEmpty()) {//如果默认的host不为空，说明是连接指定的host，如果没有指定host，使用配置中的默认host和端口
            if(port == 0) {
                logger.error("请输入服务器端口号");
                return;
            }
            gameClientConfig.setDefaultGameGatewayHost(host);
            gameClientConfig.setDefaultGameGatewayPort(port);;
        }
        gameClientBoot.launch();// 启动客户端并连接游戏网关
    }
    @ShellMethod("关闭连接")
    public void close() {
        gameClientBoot.getChannel().close();
    }

    @ShellMethod("发送测试消息，格式：send-test-msg 消息号")
    public void sendTestMsg(int messageId) {
        if(messageId == 1) {//发送认证请求
            ConfirmMesgRequest request = new ConfirmMesgRequest();
            request.getBodyObj().setToken(gameClientConfig.getGatewayToken());
            logger.info("game-token:{}",gameClientConfig.getGatewayToken());
            System.out.println("game-token:{+"+gameClientConfig.getGatewayToken()+"}");
            gameClientBoot.getChannel().writeAndFlush(request);
        }
        if(messageId == 10001) {
            // 向服务器发送一条消息
            FirstMsgRequest request = new FirstMsgRequest();
            request.setValue("Hello,server !!");
            request.getHeader().setClientSendTime(System.currentTimeMillis());
            gameClientBoot.getChannel().writeAndFlush(request);
        }
        if(messageId == 10002) {
            SecondMsgRequest request = new SecondMsgRequest();
            request.getBodyObj().setValue1("你好，这是测试请求");
            request.getBodyObj().setValue2(System.currentTimeMillis());
            gameClientBoot.getChannel().writeAndFlush(request);
        }
        if(messageId == 201) {//进入游戏请求
            EnterGameMsgRequest request = new EnterGameMsgRequest();
            gameClientBoot.getChannel().writeAndFlush(request);
        }
        if(messageId == 202){
            GetPlayerByIdMsgRequest request = new GetPlayerByIdMsgRequest();
            request.getBodyObj().setPlayerId(1);
            gameClientBoot.getChannel().writeAndFlush(request);
        }
        if(messageId == 210){
            BuyArenaChallengeTimesMsgRequest request = new BuyArenaChallengeTimesMsgRequest();
            gameClientBoot.getChannel().writeAndFlush(request);
        }
    }
}
